package com.fbli.test.controller;

import com.fbli.test.common.entity.project.*;
import com.fbli.test.common.result.ResultParam;
import com.fbli.test.common.result.ResultSend;
import com.fbli.test.entity.Project;
import com.fbli.test.entity.User;
import com.fbli.test.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = {"项目功能"})
@RestController
@RequestMapping("project")
public class ProjectController {

    @Resource
    private ProjectService projectService;


    @ApiOperation("获取用户所有项目")
    @PostMapping("getAll")
    public ResultParam getProjects(@RequestBody ProjectReqGetAllMg msg) {
        User userEntity = (User) SecurityUtils.getSubject().getPrincipal();
        ProjectResMsg resMsg = projectService.getProjects(userEntity.getId(), msg.getPage(), msg.getSize());
        if (resMsg == null) ResultSend.fail("参数错误！");
        return ResultSend.success(resMsg);
    }

    @ApiOperation("获取项目数据")
    @PostMapping("getById")
    public ResultParam getById(@RequestBody ProjectReqMsg msg) {
        User userEntity = (User) SecurityUtils.getSubject().getPrincipal();
        List<Project> detailMsgList = projectService.getById(userEntity.getId(), msg.getProjectId());
        if (detailMsgList == null) ResultSend.fail("没有id=" + msg.getProjectId() + "的项目数据");
        return ResultSend.success(detailMsgList);
    }

    @ApiOperation("增加项目")
    @PostMapping("add")
    @RequiresPermissions("admin")
    public ResultParam addProject(@RequestBody ProjectAddReqMsg msg) {
        User userEntity = (User) SecurityUtils.getSubject().getPrincipal();
        long projectId = msg.getProjectId();
        if (projectId > 0) {
            return ResultSend.fail("项目已存在！");
        } else {
            boolean success = projectService.addProject(userEntity.getId(), msg);
            if (!success) return ResultSend.fail("添加失败！");
            return ResultSend.success("添加成功！");
        }
    }
    @ApiOperation("编辑项目")
    @PostMapping("edit")
    public ResultParam editProject(@RequestBody ProjectAddReqMsg msg) {
        User userEntity = (User) SecurityUtils.getSubject().getPrincipal();
        long projectId = msg.getProjectId();
        if (projectId > 0) {
            boolean success = projectService.editProject(userEntity.getId(), msg);
            if (!success) return ResultSend.fail("编辑失败！");
            return ResultSend.success("编辑成功！");
        } else {
            return ResultSend.fail("项目不存在！");
        }
    }

    @ApiOperation("删除用户项目")
    @PostMapping("delete")
    @RequiresPermissions("admin")
    public ResultParam deleteProject(@RequestBody ProjectDeleteMsg msg) {
        User userEntity = (User) SecurityUtils.getSubject().getPrincipal();
        List<Long> projectIdList = msg.getProjectIdList();
        if (projectIdList != null && projectIdList.size() > 0) {
            boolean success = projectService.delete(userEntity.getId(), projectIdList);
            if (!success) return ResultSend.fail("删除失败！");
        }
        return ResultSend.success("删除成功！");
    }
    @ApiOperation("条件查询，根据项目id或项目名")
    @PostMapping("/selectCondition")
    //@RequiresPermissions("admin")
    public ResultParam selectCondition(@RequestBody ProjectReqConditionQuery query){
        ProjectResMsg projectResMsg = projectService.selectByIdOrByName(query);
        if (projectResMsg==null){
            return ResultSend.fail("参数不正确");
        }
        return ResultSend.success(projectResMsg);
    }
}